package com.lzy.business.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.base.dtos.SysIdentityDto;
import com.lzy.business.facade.ProductInfoFacade;
import com.lzy.business.multiple.MultipleDatasourceService;
import com.lzy.business.request.ProductInfoRequest;
import com.lzy.business.response.ProductInfoResponse;
import com.lzy.framework.component.PageBean;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.product.apis.ProductClassifyServiceApi;
import com.lzy.product.apis.ProductInfoServiceApi;
import com.lzy.product.apis.ProjectServiceApi;
import com.lzy.product.common.util.UserUtil;
import com.lzy.product.constants.BusinessType;
import com.lzy.product.dtos.ProductClassifyDto;
import com.lzy.product.dtos.ProductInfoDto;
import com.lzy.product.dtos.ProductInfoProjectDto;
import com.lzy.product.dtos.ProjectDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping(value = "/admin/product/info")
public class ProductInfoController extends BaseController{
    private static final Logger LOGGER = LoggerFactory.getLogger(ProductInfoController.class);
    @Autowired
    ProductInfoServiceApi productInfoServiceApi;

    @Autowired
    ProductInfoFacade productInfoFacade;

    @Autowired
    ProductClassifyServiceApi productClassifyServiceApi;

    @Autowired
    ProjectServiceApi projectServiceApi;

    @Resource
    MultipleDatasourceService multipleDatasourceService;

    /**
     * 产品管理列表页面
     *
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String list() {
        return "product/manage_list";
    }

    /**
     * 查询列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/json", method = RequestMethod.GET)
    @ResponseBody
    public PageBean json(ProductInfoRequest request) {
        Ensure.that(request).isNotNull("01000003");
        Pager pager = new Pager();
        pager.setPageSize(request.getPageSize());
        pager.setPageNum(request.getPageNum());
        PageInfo<ProductInfoDto> pageInfo = productInfoFacade.select(pager, DozerUtils.map(request, ProductInfoDto.class));
        if (null != pageInfo) {
            List<ProductInfoDto> list = pageInfo.getList();
            return new PageBean<ProductInfoResponse>(pageInfo.getTotal(), DozerUtils.maps(list, ProductInfoResponse.class));
        } else {
            return null;
        }
    }

    /**
     * 新增产品页面
     */
    @RequestMapping(value = "/add")
    public String add(Model model) {
        //查询产品种类
        List<ProductClassifyDto> productClassifyDtoList = productClassifyServiceApi.query(null);
        Ensure.that(productClassifyDtoList).isNotNull("02000002");
        //查询产品包含项
        ProjectDto projectDto = new ProjectDto();
        projectDto.setBusiType(BusinessType.OrderProductType.PRODUCT.getCode());
        PageInfo<ProjectDto> pageInfo  = projectServiceApi.queryList(null,projectDto);
        Ensure.that(pageInfo).isNotNull("01000006");
        Ensure.that(pageInfo.getList()).isNotNull("01000006");
        //查询身份列表
        List<SysIdentityDto> identityList = multipleDatasourceService.getList();
        Ensure.that(identityList).isNotNull("01000008");
        model.addAttribute("classifyList", productClassifyDtoList);
        model.addAttribute("projectList", pageInfo.getList());
        model.addAttribute("identityList", identityList);
        return "product/manage_add";
    }


    /**
     * 保存新增产品
     */
//    @RequiresPermissions("sys:product:add")
    @RequestMapping(value = "/save")
    @ResponseBody
    public Result save(ProductInfoRequest request) {
        Ensure.that(request).isNotNull("01000003");
        request.setCreateUser(UserUtil.getCurrentUser().getId());
        Boolean b = productInfoFacade.insert(DozerUtils.map(request, ProductInfoDto.class),getCurrentUserStr());
        Ensure.that(b).isTrue("01000001");
        return new Result();
    }

    /**
     * 产品编辑页面
     */
    @RequestMapping(value = "/edit")
    public String edit(@RequestParam("id") Long id, Model model) {
        Ensure.that(id).isNotNull("01000003");
        //查询身份列表
        List<SysIdentityDto> identityList = multipleDatasourceService.getList();
        Ensure.that(identityList).isNotNull("01000008");
        //查询产品分类
        List<ProductClassifyDto> productClassifyDtoList = productClassifyServiceApi.query(null);
        Ensure.that(productClassifyDtoList).isNotNull("02000002");
        //获取产品基本信息
        ProductInfoDto productInfoDto = productInfoFacade.findById(id);
        List<ProductInfoProjectDto> proProjectList = productInfoFacade.selectSysIdentityByProductInfo(productInfoDto);
        model.addAttribute("proList", proProjectList);
        model.addAttribute("identityList", identityList);
        model.addAttribute("classifyList", productClassifyDtoList);
        model.addAttribute("dto", productInfoDto);
        return "product/manage_add";
    }

    /**
     * 产品保存方法
     */
//    @RequiresPermissions("sys:product:update")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Result update(ProductInfoRequest request) {
        Ensure.that(request).isNotNull("01000003");
        request.setUpdateUser(UserUtil.getCurrentUser().getId());
        Boolean b = productInfoFacade.update(DozerUtils.map(request, ProductInfoDto.class),getCurrentUserStr());
        Ensure.that(b).isTrue("01000004");
        return new Result();
    }

    /**
     * 上架/下架
     */
    @RequestMapping(value = "/upDown", method = RequestMethod.GET)
    @ResponseBody
    public Result upDownProduct(@RequestParam("id") Long id, Integer shelves) {
        Ensure.that(id).isNotNull("01000003");
        Ensure.that(shelves).isNotNull("01000003");
        ProductInfoDto dto = new ProductInfoDto();
        dto.setShelves(shelves);
        dto.setId(id);
        dto.setUpdateUser(UserUtil.getCurrentUser().getId());
        Boolean b = productInfoServiceApi.update(dto);
        Ensure.that(b).isTrue("01000005");
        return new Result();
    }

    /**
     * 产品详情页面
     */
    @RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
    public String toQuery(@PathVariable("id") Long id, Model model) {
        Ensure.that(id).isNotNull("01000005");
        ProductInfoDto productInfoDto = productInfoFacade.findById(id);
        if(productInfoDto == null || productInfoDto.getDeleted()){
            return "approval/check_error";
        }
        List<ProductInfoProjectDto> proProjectList = productInfoFacade.selectSysIdentityByProductInfo(productInfoDto);
        model.addAttribute("proList", proProjectList);
        model.addAttribute("dto", productInfoDto);
        return "product/manage_view";
    }

    /**
     * 批量上架/批量下架
     */
    @RequestMapping(value = "/upDownList", method = RequestMethod.POST)
    @ResponseBody
    public Result upDownList(@RequestParam("ids") Long[] idList, Integer shelves) {
        Ensure.that(idList).isNotNull("01000003");
        Ensure.that(shelves).isNotNull("01000003");
        Boolean b = productInfoFacade.upDownList(idList, shelves);
        Ensure.that(b).isTrue("01000009");
        return new Result();
    }

}
